import {
  createStore
} from 'vuex'

export default createStore({
  state: {
    user: null
  },
  getters: {
    user: state => {
      if (state.user == null) {
        try {
          return JSON.parse(sessionStorage.getItem('user'))
        } catch (e) {
          state.user = {};
        }
      }
      return state.user;
    }
  },
  mutations: {
    setUser: (state, user) => {
      if (user == null) {
        sessionStorage.removeItem('user');
        sessionStorage.removeItem('token');
      } else {
        sessionStorage.setItem('user', JSON.stringify(user));
      }
      state.user = user;
    }
  }
})